import { onMounted, reactive } from 'vue';
import { allInstance, findTaskInfo, findUserTask } from '@api/url/processManagement';
import router from '@/router';

export default function importConfig() {
  const base = reactive<any>({
    pagination: {
      total: 100, //总条数
      current: 1,
      pageSize: 10,
    },
    tabelList: null,
    checkFrom: {
      customName: '',
      deliveryDate: '',
      orderDate: '',
      orderId: '',
      orderType: '',
      userId: '',
    },
    rowSelection: {
      type: 'checkbox',
      showCheckedAll: true,
      onlyCurrent: false,
    },
    expandable: {
      title: '详情',
      width: 80,
      expandedRowRender: (record): any => {
        return record.tc001 + record.tc002;
      },
    },
    taskID: null,
    taskData: null,
  });

  /**
   * @exports getTable 销售订单列表数据获取
   */
  const getTable = () => {
    const obj = {
      size: base.pagination.pageSize,
      current: base.pagination.current,
    };

    findUserTask(obj).then((res: any) => {
      if (res.code == '200') {
        base.tabelList = res.data.list;
        base.tabelList.map((rev: any, index: any) => {
          rev.key = index;
        });
        base.pagination.total = res.data.total;
      }
    });
  };

  //保存当前行数据详情
  const storeData = () => {
    const obj = {
      taskID: base.taskID,
      data: base.taskData,
    };
    localStorage.setItem('currentUserTaskData', JSON.stringify(obj));
  };

  //查看详情
  const toDetail = (taskId) => {
    base.taskID = taskId;
    const obj = {
      taskID: taskId,
    };
    findTaskInfo(obj).then((res) => {
      if (res.code == 200) {
        base.taskData = res.data;
        storeData();
        if (res.data.mark == 'purtc') {
          router.push({
            path: '/purchaseOrderDetails',
          });
        } else if (res.data.mark == 'coptc') {
          router.push({ path: '/viewSalesDetails' });
        }
      }
    });
  };

  onMounted(() => {
    localStorage.removeItem('currentUserTaskData');
    getTable();
  });

  return {
    base,
    getTable,
    toDetail,
  };
}
